<?php
require_once("includes/pagetop.php");

if(!(isset($_SESSION['Allyfolio_User_ID']) && trim($_SESSION['Allyfolio_User_ID'])!=""))
{
    redirect("index.php");
    exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>AllyFolio | SignUp </title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"  />


</head>
<style type="text/css">
	table tr td div{ text-align:left !important; margin:0 !important}
	table tr{ vertical-align:top; padding:10px;}
</style>

<script type="text/javascript" src="js/jquery_1.9.0_min.js"> </script>
<script type="text/javascript" src="js/script.js"></script>

<script type="text/javascript" src="js/general.js"></script>

 <script>
$(document).ready(function(){
  $(".drop_btn").click(function(){
    $(".showbox").toggle();
  });
});
</script>

<link rel="stylesheet" href="alertify/themes/alertify.core.css" />
<link rel="stylesheet" href="alertify/themes/alertify.default.css" id="toggleCSS" />
<script src="alertify/lib/alertify.min.js"></script>

<body>
<div class="" align="center"><a href="#" class="topopup">Friend Request</a></div>
    <?php
    $condition=array();
    $query="select af_users.*,Unique_User_Friend_ID from af_users,af_user_friends where af_users.User_ID=af_user_friends.User_ID and af_user_friends.status=3 and User_Friend_ID='".$_SESSION['Allyfolio_User_ID']."' ";
        
    $query.=" order by requestdate desc " ;
    
    $friendsinfo=$pdodbobj->fetch_array($pdodbobj->query($query,array()));
    
    ?>
    <div id="toPopup" class="friendReq"> 
    	<div class="close"></div>
            <div id="popup_content">  
                <div id="main_wrapper">
         		<div class="new_inb3"  style="width:auto;">
        			<div style="width:462px; margin:0 auto" >
        			<div style="width:462px; margin:0 auto; position:relative; font-size:16px; color:#44c8f5">
        			<div style="float:left">Friend Request</div>
        				<div style="float:left; margin-left:90px;"><img src="images/arrow.png" style="" /></div>
        				<div style="float:right; margin-right:5px; font-size:18px; font-weight:bold;">Settings</div>
        			    <div style="clear:both"></div>
        			</div>
        			
        			<table border="0" cellpadding="0" cellspacing="10" width="460px"  style="border:1px solid #abe1fa; padding:10px;" class="notu">
                    <?php
                            if(count($friendsinfo)>0)
                            {
                                for($i=0;$i<count($friendsinfo);$i++)
                                {
                            ?>
                            <tr>
                			    <td width="60" style="padding:0px;"><div class="noti_pic"><a href="dna.php?t=<?php echo $friendsinfo[$i]['Allyfolio_User_ID']; ?>"> <img class="userpiclass" src="<?php if(trim($friendsinfo[$i]['Profile_Pic_Path'])!=""){ echo $friendsinfo[$i]['Profile_Pic_Path']; }else{ echo "images/default_user_pic.jpg"; } ?>" height="48px" width="45px" /></a></div></td>
                				<td width="220" valign="top" style="text-align:left;">
                				<div style="position:relative; top:-4px"><span class="noti_name" style="font-size:21px; color:#44c8f5 !important"><a href="dna.php?t=<?php echo $friendsinfo[$i]['Allyfolio_User_ID']; ?>"><?php echo $friendsinfo[$i]['Full_Name']; ?></p></a></span></div>
                				<div class="separator"></div>
                	               <span style="margin-top:5px; float:left; margin-right:5px; color:#44c8f5 !important"><?php echo getCommonFriends($pdodbobj,$friendsinfo[$i]['User_ID'],$_SESSION['Allyfolio_User_ID'])." Common Friends"; ?></span>
                                </td>
                				<td width="138" align="right" id="requesttd_<?php echo $i; ?>">
                				
                                	<table border="0" width="100%">
                                	   <tr>
                                			<td rowspan="2" valign="middle"><input type="button" class="reject" value="Accept" onclick="return changeStatusFriendRequest(1,<?php echo $friendsinfo[$i]['Unique_User_Friend_ID']; ?>,<?php echo $i; ?>);" /></td>
                                			<td><input type="button" class="reject" value="Reject"  onclick="return changeStatusFriendRequest(4,<?php echo $friendsinfo[$i]['Unique_User_Friend_ID']; ?>,<?php echo $i; ?>);" /></td>
                                		</tr>
                                		<tr>
                                			<td><input type="button" class="reject" value="Block"  onclick="return changeStatusFriendRequest(0,<?php echo $friendsinfo[$i]['Unique_User_Friend_ID']; ?>,<?php echo $i; ?>);" /></td>
                                		</tr>
                                	</table>
                			   </td>
                			</tr>
                            <?php
                                }
                            }
                            else
                            {
                            ?>
                			<tr>
                			  <td>
                			  	<div style="height:105px" style="color: red;">No Friend Request!</div>
                			  </td>
                			</tr>
                            <?php
                            }
                            ?>
        			</table>
        			<div style="width:100%; text-align:right !important; color:#4ca1d4; width:462px; margin:0 auto;">Settings</div>
        			</div>
        	
        		</div>
        		
        		<br />
                <?php
                $query="select distinct(af_users.User_ID),Profile_Pic_Path,Full_Name,Allyfolio_User_ID from af_users,af_user_friends where Profile_Status=0 and (((af_users.User_ID=af_user_friends.User_ID) or (af_users.User_ID=af_user_friends.User_Friend_ID)) and af_user_friends.status=3) and af_users.User_ID not in ( select Active_Friends_IDs from af_users where User_ID='".$_SESSION['Allyfolio_User_ID']."') and af_users.User_ID!='".$_SESSION['Allyfolio_User_ID']."'
                            AND af_users.User_ID not in ( select User_ID from af_user_friends where User_Friend_ID='".$_SESSION['Allyfolio_User_ID']."' )
                             AND af_users.User_ID not in ( select User_Friend_ID from af_user_friends where User_ID='".$_SESSION['Allyfolio_User_ID']."' ) 
                            ";
        
                $query.=" order by Full_Name asc limit 10" ;
                
                $knownfriendsinfo=$pdodbobj->fetch_array($pdodbobj->query($query,array()));
                
                if(count($knownfriendsinfo)==0)
                {
                    $query="select distinct(af_users.User_ID),Profile_Pic_Path,Full_Name,Allyfolio_User_ID from af_users where Profile_Status=0 and af_users.User_ID!='".$_SESSION['Allyfolio_User_ID']."'
                            AND af_users.User_ID not in ( select User_ID from af_user_friends where User_Friend_ID='".$_SESSION['Allyfolio_User_ID']."' )
                             AND af_users.User_ID not in ( select User_Friend_ID from af_user_friends where User_ID='".$_SESSION['Allyfolio_User_ID']."' ) 
                            ";
        
                    $query.=" order by rand(), Full_Name asc limit 10" ;
                    $knownfriendsinfo=$pdodbobj->fetch_array($pdodbobj->query($query,array()));
                    
                }
                if(count($knownfriendsinfo)>0)
                {
                ?>
        		<div class="new_inb3" style="width:auto;">
        			<div style="width:462px; margin:0 auto" >
        				<table border="0" cellpadding="0" cellspacing="10" width="460px"  style="border:1px solid #abe1fa; padding:10px;" class="notu">
                        <?php
                            
                                for($i=0;$i<count($knownfriendsinfo);$i++)
                                {
                                    
                            ?>
                			<tr>
                                <td width="60" style="padding:0px;"><div class="noti_pic"><a href="dna.php?t=<?php echo $knownfriendsinfo[$i]['Allyfolio_User_ID']; ?>"> <img class="userpiclass" src="<?php if(trim($knownfriendsinfo[$i]['Profile_Pic_Path'])!=""){ echo $knownfriendsinfo[$i]['Profile_Pic_Path']; }else{ echo "images/default_user_pic.jpg"; } ?>" height="48px" width="45px" /></a></div></td>
                				<td width="220" valign="top" style="text-align:left;">
                				<div style="position:relative; top:-4px"><span class="noti_name" style="font-size:21px; color:#44c8f5 !important"><a href="dna.php?t=<?php echo $knownfriendsinfo[$i]['Allyfolio_User_ID']; ?>"><?php echo $knownfriendsinfo[$i]['Full_Name']; ?></p></a></span></div>
                				<div class="separator"></div>
                	               <span style="margin-top:5px; float:left; margin-right:5px; color:#44c8f5 !important"><?php echo getCommonFriends($pdodbobj,$knownfriendsinfo[$i]['User_ID'],$_SESSION['Allyfolio_User_ID'])." Common Friends"; ?></span>
                                </td>
                				<td width="138" align="right" id="sendreqtd_<?php echo $i; ?>">
                					<table border="0" width="100%">
                                	   <tr>
                                			<td rowspan="2" valign="middle">&nbsp;</td>
                                			<td align="right"><input type="button" class="reject" value="Add Friend"  style="width:90px" <input type="button" class="reject" value="Accept" onclick="return sendFriendRequest(<?php echo $knownfriendsinfo[$i]['User_ID']; ?>,<?php echo $i; ?>);"  /></td>
                                		</tr>
                                		<tr>
                                			<td>&nbsp;</td>
                                		</tr>
                                	</table>
                            	</td>
                			</tr>
                			<?php
                            }
                            ?>
                			<!--<tr>
                				<td colspan="4"><div style="width:100%; text-align:center !important; color:#4ca1d4; margin:0 auto;"><a href="#">More...</a></div></td>
                			</tr>-->
                            
            			</table>
        			
        			</div>
        	
        		</div>
                <?php
                }
                ?>
        	</div> 
        </div>
    </div>
    <div class="loader"></div>
   	<div id="backgroundPopup"></div>
	
</body>
</html>
<script lang="javascript">
//<![CDATA[

var requeststarr=new Array();
requeststarr[1]="Accepted";
requeststarr[0]="Blocked";
requeststarr[4]="Rejected";
function changeStatusFriendRequest(status,recordid,rowid)
{
    if(recordid>0 && document.getElementById('requesttd_'+rowid))
    {
        $.ajax({
          cache:false,  
          async:false,
          url: "ajax_search/ajax_manageRequests.php?action=changereqstatus&recordid="+recordid+"&status="+status,
    	  success: function( data ) {
    	    if(trim(data)=="success")
            {
                document.getElementById('requesttd_'+rowid).innerHTML="<b>"+requeststarr[status]+"</b>";
                return false;
            }
            
    	  }
    	});
    }
}
    
function sendFriendRequest(touserid,rowid)
{
    alertify.confirm("Are you sure, you want to send friend request?", function (e) {
		if (e) {
			alertify.success("You've clicked OK");
			$.ajax({
              cache:false,  
              async:false,
              url: "ajax_search/ajax_manageRequests.php?action=friendrequest&touserid="+touserid,
        	  success: function( data ) {
        	    if(trim(data)=="success")
                {
                    alertify.alert("You have successfully sent friend request.");
                    document.getElementById('sendreqtd_'+rowid).innerHTML="<b>Request Sent.</b>";
                    return false;
                }
                
        	  }
        	});
		} else {
			alertify.error("You've clicked Cancel");
		}
	});
	
    
    
}

//]]>
</script>